package actions;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.MultipartRequest;

import beans.User;
import beans.UserDAO;

public class UserActionRegister implements UserAction {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException {
		boolean flag;
		UserDAO userdao=new UserDAO();
		//flag=userdao.insertphoto(request);
		User user=receivealldata(request);//接所有数据，包括文件
		flag=userdao.insert(user);//调用原来的方法，向数据库写入数据
		if(flag){
			request.getSession().setAttribute("user", user);//保存user,以便于到了main.jsp中可以取出其中的照片
			response.sendRedirect("main.jsp");
		}else{
			response.sendRedirect("error.jsp");
		}
	}
	
	public User receivealldata(HttpServletRequest request) {
		boolean flag=false;		
		//1定义保存图片的目录
		String saveDirectory = request.getSession().getServletContext().getRealPath("")+"\\upload";
		File savedir = new File(saveDirectory);//File可表示目录，也可表示文件
		if (!savedir.exists()) { savedir.mkdirs();}
		
		//2 定义MultipartRequest对象，接各类参数，保存上传的文件
		int maxPostSize = 5 * 1024 * 1024; // 什么意思？5M
		MultipartRequest multiRequest=null;	
		try {
			multiRequest = new MultipartRequest(request,saveDirectory, maxPostSize, "utf-8");
		} catch (IOException e) {			
			e.printStackTrace();
		}
		
		//String userno = multiRequest.getParameter("userno");//先取一个用户号备用，因为要求保存的文件名是:用户号.png
		
		
		String userphoto="";//如果没有传照片，则用空来表示
		Enumeration<String> userFileEnumeration = multiRequest.getFileNames();//标记名列表
		while(userFileEnumeration.hasMoreElements()) {//实际只传一个图片文件，改成if也可
			String htmlname = userFileEnumeration.nextElement();//htmlname是html的name属性，如userphoto
			File sendFile=multiRequest.getFile(htmlname);//是接收到的真正的文件
			String sendFileName = multiRequest.getFilesystemName(htmlname);//是接收到的真正的文件名，如test.png
			//测试接到的文件
			System.out.println("测试接到的文件:"+sendFileName);
			userphoto=sendFileName;
			
			/*
			 * 下面这段是改名，可以暂且不看
			String suffix = sendFileName.substring(sendFileName.lastIndexOf(".") + 1);//取文件的扩展名，如png
			File oldFile=new File(saveDirectory+"\\"+userno+"."+suffix);//如果已经上传过文件，则要删除，将将新传来文件名改成：用户号.png
			if(oldFile.exists()){
				oldFile.delete();						
			}
			sendFile.renameTo(oldFile);	
			userphoto = userno+"."+suffix;	
			结束，上面这段是改名，可以暂且不看
			*/
		}
			
		//接其他参数
		String userno = multiRequest.getParameter("userno");
		request.getSession().setAttribute("userno", userno);
		String userpwd = multiRequest.getParameter("userpwd");
		String username = multiRequest.getParameter("username");
		String usersex = multiRequest.getParameter("usersex");
		String userage = multiRequest.getParameter("userage");
		String useraddress = multiRequest.getParameter("useraddress");
		String userprofession = multiRequest.getParameter("userprofession");
		String useremail = multiRequest.getParameter("useremail");
		String usersummary = multiRequest.getParameter("usersummary");
		
		String []userinterests = multiRequest.getParameterValues("userinterest");
		String userinterest="";
		for(int i=0;i<userinterests.length;i++){
			userinterest=userinterest+userinterests[i]+"-";
		}
		userinterest=userinterest.substring(0, userinterest.length()-1);
		
		//3根据接到的数据，而产生user对象
		User user=new User(0,userno, userpwd, username, usersex, userage, useraddress, userprofession, userinterest, useremail, userphoto, usersummary);
		
		return user;
		
	}
	
	
	

}
